
<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-03-24 17:25:39
 * @version 1.0
 */

class ReplyAction extends AdminAction {
    
    public function index(){

        $obj = M('Autoreply');
        $count = $obj->where($map)->count();
        import("@.ORG.Page");
        $page = new Page($count, 20);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }    

    public function add(){
    	$this->display();
    }

    public function save(){
        $this->all_add('Autoreply',U('reply/index'));
    }

    public function edit(){

        $id = (int) $_GET['id'];
        if (!isset ($id)) {
            $this->error('参数错误');
        }

        $obj = M('Autoreply');

        $list = $obj->where("id=$id")->find();              

        if (!$list) {
            $this->error('信息不存在');
        } else {
            if($list['article']!=''){
                $list['article'] = explode(",",$list['article']);
                foreach($list['article'] as $key => $value){
                    $obj = M('Article');
                    $res = $obj ->where('id='.$value)->find();
                    if($res){
                    $artStr .= '<li id="aID'.$res['id'].'"><img src="'.$res['picname'].'"/><p>'.$res['title'].'</p> <input type="hidden" name="articleID[]" value="'.$res['id'].'" /> <a href="javascript:removeDiv('.$res['id'].')">删除</a></li>';    
                    }                    
                }
            }
            $this->assign('list', $list);
            $this->assign('artStr',$artStr);


            $this->display();
        }
    }

    public function update(){
        $this->all_save('Autoreply',U('Reply/index'));
    }

    public function del() {

        $id=I('post.selectedids');
        if($id==''){
            $state = "您没有选择任何信息！";
        }else{
            foreach ($id as $v) {
                $obj = M('Autoreply');
                $where['id'] = $v;
                $obj->where($where)->delete();
            }
            $state = 'SUCCESS';
            $message = '删除成功';
            $url = U('Reply/index');
        }
        echo $this->echo_json_str($state,$message,$url);
    }

    public function select(){
        $cate = M('Category')->getField('id,name');
        $path = I('get.path');
        $key  = I('get.key');

        $article = M('Article');
        if($path!=''){
            $map['path'] = array('like', $path.'%');
        }

        if($key!=''){
            $map['title'] = array('like', '%'.$key.'%');
        }

        $count = $article->where($map)->count();

        import("@.ORG.Page");

        $page = new Page($count, 10);

        $show = $page->show();

        $list = $article->where($map)->order('sort asc , id desc')->limit($page->firstRow . ',' . $page->listRows)->select();

        $artClass = M('Category');

        $sclass = $artClass->field("id,name,fid,path")->order('path')->select();

        foreach ($sclass as $key => $value) {

            $count = count(explode('-', $value['path'])) - 3;

            $sclass[$key]['count'] = $count;

        }

        $this->assign('sclass', $sclass);
        $this->assign('list', $list);
        $this->assign('cate', $cate);
        $this->assign('page', $show);
        $this->assign('path', $path);
        $this->display();
    }
}